/*
 * process.c
 *
 *  Created on: Feb 29, 2012
 *      Author: User
 */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "process.h"

/**
 * Constructor
 * Returns a new process.
 */
process* process_create(const int id,
					    const int num_steps,
					    const int num_requests,
					    const process_type type_of_process,
					    const request** the_requests){
	process* my_process = NULL;
	my_process = (process*) malloc(sizeof(process));

	if(my_process) {
		my_process->process_id = id;
		my_process->total_num_steps = num_steps;
		my_process->num_requests = num_requests;
		my_process->process_t = type_of_process;
		my_process->requests = the_requests;
	}

	return my_process;
}

/**
 * Deconstruct process.
 * Deallocates the memory from the given process pointer and sets the given
 * process pointer to NULL.
 */
int process_destroy(process* my_process) {
	free(my_process);
	my_process = NULL;

	return 0;
}

/**
 * Returns the index of the request if a request is found,
 * otherwise returns 0.
 */
int process_is_interrupted(const process* my_process,
					   const int instruction_number){
	request** requests_ptr = my_process->requests; //returns the pointer to the request array for this process.
	printf("My number of requests: %i\\t My pid: %d\n", my_process->num_requests, my_process->process_id);
	for(int i = 0; i < my_process->num_requests; i++){
		//printf("--I'm inside the loop.--\n");
		//printf("--inst. # %i, step # %i\n", instruction_number, requests_ptr[i]->step);
		if(requests_ptr[i]->step == instruction_number){
			printf("--inst. # %i, step # %i\n", instruction_number, requests_ptr[i]->step);
			return i; //found matching step number.
		}
	}
	return -1; //did not find a matching step number;
}

int process_get_request_type(const process* my_process,
							   const int index) {
	request** requests = my_process->requests;
	request* r = requests[index];

	printf("My pid is: %d", my_process->process_id);
	printf("\tMy request type is: %i\n", r->type_of_request);

	return r->type_of_request;
}
